package top.ffhome.system.config;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;
import top.ffhome.common.result.Result;
import top.ffhome.common.result.ResultCode;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
	/**
	 * 全局异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result exceptionHandler(HttpServletRequest request, Exception e) {
		log.warn("出错了！", e);

		ResultCode code;
		if (e instanceof org.springframework.web.servlet.NoHandlerFoundException) {
			code = ResultCode.NOT_FOUND;
		} else {
			code = ResultCode.SERVER_ERROR;
		}
		return Result.build(code, e.getMessage(), request.getRequestURI());
	}
}
